﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using DTO;
using System.Data;

namespace BUS
{
    public class CSendKeys
    {

        private static void SendKeyPress(byte Key)
        {
            CWindowsAPI.keybd_event(Key, 0, 0, 0);
            CWindowsAPI.keybd_event(Key, 0, CWindowsAPI.KEYEVENTF_KEYUP, 0);
        }

        private static void SendKeyPress(byte Key, int Delay)
        {
            CWindowsAPI.keybd_event(Key, 0, 0, 0);
            Thread.Sleep(Delay);
            CWindowsAPI.keybd_event(Key, 0, CWindowsAPI.KEYEVENTF_KEYUP, 0);
        }

        public static void SendKeyDown(byte Key)
        {
            CWindowsAPI.keybd_event(Key, 0, 0, 0);
        }

        public static void SendKeyUp(byte Key)
        {
            CWindowsAPI.keybd_event(Key, 0, CWindowsAPI.KEYEVENTF_KEYUP, 0);
        }

        /*public static void SendKey(CKeys key)
        {

            string[] _KeyUp = key.Shortcutkey.Split('+');
            for (int i = 0; i < _KeyUp.Length - 1; i++)
            {
                SendKeyUp((byte)BUS.CTransferKeysCode.SwithCode(_KeyUp[i].Trim()));
            }
            string[] _Keys = key.Runningkey.Split('>','>');
            int _Delay=key.Delay;
            for (int i = 0; i < _Keys.Length; i++)
            {
                if (i % 2 == 1)
                {
                    string[] _temp = _Keys[i].Split('=');
                    if (_temp.Length > 1)
                    {
                        if (_temp[0][0] == '/')
                            _Delay = key.Delay;
                        else
                            _Delay = Convert.ToInt32(_temp[1]);
                    }
                    else if(_Keys[i][0]=='/')
                        SendKeyUp((byte)CTransferKeysCode.SwithCode(_Keys[i].Remove(0,1)));
                    else
                        SendKeyDown((byte)CTransferKeysCode.SwithCode(_Keys[i]));
                    Thread.Sleep(_Delay);
                }
                else
                {
                    if (_Keys[i] != "")
                    {
                        string[] _Key = _Keys[i].Split('(',')');
                        for (int j = 0; j < _Key.Length; j++)
                        {
                            if (j % 2 == 1)
                            {
                                string[] _temp = _Key[j].Split('=');
                                if (_temp.Length > 1)
                                {
                                    if (_temp[0] == "Delay")
                                    {
                                        Thread.Sleep(Convert.ToInt32(_temp[1]));
                                    }
                                    else
                                    {
                                        SendKeyPress((byte)CTransferKeysCode.SwithCode(_temp[0]), Convert.ToInt32(_temp[1]));
                                        Thread.Sleep(_Delay);
                                    }
                                }
                                else
                                {
                                    SendKeyPress((byte)CTransferKeysCode.SwithCode(_Key[j]),_Delay);
                                    Thread.Sleep(_Delay);
                                }
                            }
                            else
                            {
                                foreach (char Key in _Key[j])
                                {
                                    SendKeyPress((byte)CTransferKeysCode.SwithCode(Key.ToString()), _Delay);
                                    Thread.Sleep(_Delay);
                                }
                            }
                        }
                    }
                }
            }
        }*/

        private static DataTable ListSend(CKeys key)
        {
            DataTable _List = new DataTable();
            _List.Columns.Add("Type",Type.GetType("System.Int16"));
            _List.Columns.Add("KeySend", Type.GetType("System.Int32"));
            _List.Columns.Add("Delay", Type.GetType("System.Int32"));
            DataRow _Row;

            string[] _Keys = key.Runningkey.Split('>', '<');
            int _Delay = key.Delay;
            for (int i = 0; i < _Keys.Length; i++)
            {
                if (i % 2 == 1)
                {
                    if (_Keys[i][0] != '/')
                    {
                        string[] _temp = _Keys[i].Split('=');
                        if (_temp[0].ToUpper() == "Delay".ToUpper())
                        {
                            _Delay = Convert.ToInt32(_temp[1]);
                        }
                        else
                        {
                            _Row = _List.NewRow();
                            _Row["Type"] = 0;
                            _Row["KeySend"] = CTransferKeysCode.SwithCode(_temp[0]);
                            _Row["Delay"] = _Delay;
                            _List.Rows.Add(_Row);
                        }
                    }
                    else
                    {
                        if (_Keys[i].Remove(0, 1).ToUpper() == "Delay".ToUpper())
                        {
                            _Delay = key.Delay;
                        }
                        else
                        {
                            _Row = _List.NewRow();
                            _Row["Type"] = 1;
                            _Row["KeySend"] = CTransferKeysCode.SwithCode(_Keys[i].Remove(0, 1));
                            _Row["Delay"] = _Delay;
                            _List.Rows.Add(_Row);
                        }
                    }
                }
                else
                {
                    if (_Keys[i] != "")
                    {
                        string[] _Key = _Keys[i].Split('(', ')');
                        for (int j = 0; j < _Key.Length; j++)
                        {
                            if (j % 2 == 1)
                            {
                                string[] _temp = _Key[j].Split('=');
                                if (_temp.Length > 1)
                                {
                                    if (_temp[0].ToUpper() == "Delay".ToUpper())
                                    {
                                        _List.Rows[_List.Rows.Count-1]["Delay"] = Convert.ToInt32(_temp[1]);
                                    }
                                    else
                                    {
                                        _Row = _List.NewRow();
                                        _Row["Type"] = 0;
                                        _Row["KeySend"] = CTransferKeysCode.SwithCode(_temp[0]);
                                        _Row["Delay"] = Convert.ToInt32(_temp[1]);
                                        _List.Rows.Add(_Row);
                                        _Row = _List.NewRow();
                                        _Row["Type"] = 1;
                                        _Row["KeySend"] = CTransferKeysCode.SwithCode(_temp[0]);
                                        _Row["Delay"] = Convert.ToInt32(_Delay);
                                        _List.Rows.Add(_Row);
                                    }
                                }
                                else
                                {
                                    _Row = _List.NewRow();
                                    _Row["Type"] = 0;
                                    _Row["KeySend"] = CTransferKeysCode.SwithCode(_Key[j]);
                                    _Row["Delay"] = Convert.ToInt32(_Delay);
                                    _List.Rows.Add(_Row);
                                    _Row = _List.NewRow();
                                    _Row["Type"] = 1;
                                    _Row["KeySend"] = CTransferKeysCode.SwithCode(_Key[j]);
                                    _Row["Delay"] = Convert.ToInt32(_Delay);
                                    _List.Rows.Add(_Row);
                                }
                            }
                            else
                            {
                                foreach (char Key in _Key[j])
                                {
                                    _Row = _List.NewRow();
                                    _Row["Type"] = 0;
                                    _Row["KeySend"] = CTransferKeysCode.SwithCode(Key.ToString());
                                    _Row["Delay"] = Convert.ToInt32(_Delay);
                                    _List.Rows.Add(_Row);
                                    _Row = _List.NewRow();
                                    _Row["Type"] = 1;
                                    _Row["KeySend"] = CTransferKeysCode.SwithCode(Key.ToString());
                                    _Row["Delay"] = Convert.ToInt32(_Delay);
                                    _List.Rows.Add(_Row);
                                }
                            }
                        }
                    }
                }
            }

            return _List;
        }

        int _KeyDownNext=-1;
        int _KeyUpNext = -1;

        public int KeyDownNext
        {
            get { return _KeyDownNext; }
            set { _KeyDownNext = value; }
        }

        public int KeyUpNext
        {
            get { return _KeyUpNext; }
            set { _KeyUpNext = value; }
        }

        public void SendKey(CKeys key)
        {
            //string[] _KeyUp = key.Shortcutkey.Split('+');
            //for (int i = 0; i < _KeyUp.Length - 1; i++)
            //{
            //    KeyUpNext =BUS.CTransferKeysCode.SwithCode(_KeyUp[i].Trim());
            //    SendKeyUp((byte)_KeyDownNext);
            //}
            DataTable _ListKey = ListSend(key);

            foreach(DataRow _Row in _ListKey.Rows)
            {
                if (Convert.ToInt16(_Row["Type"]) == 0)
                {
                    _KeyDownNext = (int)_Row["KeySend"];
                    SendKeyDown(Convert.ToByte(_KeyDownNext));
                }
                else
                {
                    _KeyUpNext = (int)_Row["KeySend"];
                    SendKeyUp(Convert.ToByte(_KeyUpNext));
                }
                Thread.Sleep((int)_Row["Delay"]);
            }
        }

        public void SendKeyUp(string Shortcutkey)
        {
            //foreach (int Key in KeyUp)
            //{
            //    KeyDownNext = Key;
            //    SendKeyUp((Byte)Key);
            //}

            string[] _KeyUp = Shortcutkey.Split('+');
            for (int i = 0; i < _KeyUp.Length - 1; i++)
            {
                KeyUpNext = BUS.CTransferKeysCode.SwithCode(_KeyUp[i].Trim());
                SendKeyUp((byte)_KeyUpNext);
            }
        }
    }
}